import { ref } from "vue"
import { searchStore } from "@/stores/searchDate"
import { requestSearch } from "@/api/search"


export const useSearch = ()=>{
    const list:any = ref([])
    const searchValue = ref('')
    const visible = ref(true)
    
    // 节流处理
    let flag = ref(true)
    const goodsSearch = () => {
        
        if (searchValue.value !== '') {
            
            if (flag) {
                
                flag.value = false
                setTimeout(() => {
                    const bindSearch = async () => {
                        const data: any = await requestSearch(searchValue.value)
                        const { resultCode, resultInfo } = data
                        if (resultCode === 1) {

                            list.value = []
                            const count = searchStore()
                            count.saveSearchList(resultInfo.list)
                            
                            list.value = count.getSearchList
                            visible.value = false
                        }else{
                            list.value = []
                            visible.value = true
                        }
                    }
                    bindSearch()
                    flag.value = true
                }, 500)
            }
        }else{
            
             visible.value = true
        }
    }
    return { goodsSearch, searchValue,list, visible }
}
